package com.example.message.service;

import com.example.message.config.MessageQueue;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * Author:   dgc
 * Date:     2020/8/12 15:57
 */
@Service
@Slf4j
public class MessageService {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    public Object sendMessage(String msg,Integer time){

        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setCorrelationId(UUID.randomUUID().toString());
        messageProperties.setHeader("x-delay",time);
        log.info("队列:{},在{}ms后执行", msg,time);
        Message message = new Message(msg.getBytes(), messageProperties);
        rabbitTemplate.convertAndSend(MessageQueue.EXCHANGE, MessageQueue.ROUTINGKEY1, message);
        return null;
    }
}
